package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.Pipeline;
import com.sandking.db.bean.Yhfb;
import redis.clients.jedis.Jedis;
import com.sandking.db.dao.YhfbDao;

public class YhfbJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhfb getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhfb yhfb = null;
		try{
			String key = "Yhfb_Object";
			String field = SK_Plus.b("id:",id).e();
			yhfb = Yhfb.createForJson(jedis.hget(key,field));
			if(yhfb == null){
				yhfb = YhfbDao.getById(id);
				if(yhfb!=null){
					loadCache(yhfb);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhfb;
	}
	
	/**
	 * 根据( 副本id  用户_id ) 查询
	 */
	public static Yhfb getByFbidYh_id(int fbid, int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhfb yhfb = null;
		try{
			String key = "Yhfb_Index";
			String field = SK_Plus.b("fbid, yh_id:",fbid, yh_id).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				yhfb = getById(Integer.valueOf(primaryKey));
			}
			if(yhfb == null){
				yhfb = YhfbDao.getByFbidYh_id(fbid, yh_id);
				if(yhfb!=null){
					loadCache(yhfb);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhfb;
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static List<Yhfb> getByYh_id(int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhfb> yhfbs = new ArrayList<Yhfb>();
		try{
			String key = "Yhfb_Index";
			String field = SK_Plus.b(key,"yh_id:",yh_id).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhfb_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhfbs = Yhfb.createForJson(jsons);
			}else{
				yhfbs = YhfbDao.getByYh_id(yh_id);
				if(yhfbs!=null && !yhfbs.isEmpty()){
					loadCaches(yhfbs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhfbs;
	}
	

	public static void loadCache(Yhfb yhfb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhfb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhfb yhfb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhfb_Object";
		String field = SK_Plus.b("id:",yhfb.getId()).e();
		String data = yhfb.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(yhfb.getId());
		key = "Yhfb_Index";
		
		field = SK_Plus.b("fbid, yh_id:",yhfb.getFbid(),yhfb.getYh_id()).e();
		p.hset(key,field,primaryKey);
		
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"yh_id:",yhfb.getYh_id()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhfb> yhfbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhfb yhfb : yhfbs){
				loadCache(yhfb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhfb yhfb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhfb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhfb yhfb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhfb_Object";
		String field = SK_Plus.b("id:",yhfb.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Yhfb_Index";
		
		field = SK_Plus.b("fbid, yh_id:",yhfb.getFbid(),yhfb.getYh_id()).e();
		p.hdel(key,field);
		
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(yhfb.getId());
		field = SK_Plus.b(key,"yh_id:",yhfb.getYh_id()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhfb> yhfbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhfb yhfb : yhfbs){
				clearCache(yhfb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhfb insert(Yhfb yhfb){
		yhfb = YhfbDao.insert(yhfb);
		if(yhfb!=null){
			loadCache(yhfb);
		}
    	return yhfb;
    }
    
    public static Yhfb update(Yhfb yhfb){
    	yhfb = YhfbDao.update(yhfb);
    	if(yhfb!=null){
			loadCache(yhfb);
		}
    	return yhfb;
    }
    
    public static boolean delete(Yhfb yhfb){
    	boolean bool = YhfbDao.delete(yhfb);
    	if(bool){
    		clearCache(yhfb);
    	}
    	return bool;
    }
}